/*****************************************************************************\

File:   otkUDF.java

Date          Build     Author        Version   Comments
18-Sep-12    P-20-14    asonar         $$1       Created
20-mar-15 P-30-05 gshmelev $$2  called GetCurrentSession


\****************************************************************************/

package com.ptc.otkjavaexamples;

import com.ptc.cipjava.*;
import com.ptc.pfc.pfcSession.*;
import com.ptc.pfc.pfcCommand.*;
import com.ptc.pfc.pfcGlobal.*;
import com.ptc.pfc.pfcModel.*;
import com.ptc.pfc.pfcModelItem.*;
import com.ptc.pfc.pfcFeature.*;
import com.ptc.pfc.pfcSolid.*;
import java.io.*;

import com.ptc.pfc.pfcFamily.*;
import com.ptc.wfc.wfcSession.*;
import com.ptc.pfc.pfcWindow.*;
import com.ptc.pfc.pfcUDFCreate.*;
import com.ptc.pfc.pfcGeometry.*;
import com.ptc.pfc.pfcSelect.*;
import com.ptc.wfc.wfcSolid.*;

/*=====================================================================================*\
  FUNCTION : addUDF()
  PURPOSE  : This example places copies of a node UDF at a particular coordinate system
  location in a part.  The node UDF is a spherical cut centered at the
  coordinate system whose diameter is driven by the 'diam' argument to the
  method.     
  The method returns the FeatureGroup object created, or null if an error
  occurred.
  \*=====================================================================================*/

class otkUDF extends DefaultUICommandActionListener
{
    
    public otkUDF()
    {
    }
    
    public void OnCommand()
    {
	addUDF();
    }
    
    public void addUDF()
    {	
	try
	    {
		
		Session session = pfcSession.GetCurrentSession ();
		WSession ses = (WSession)session;
		
		Model model = ses.GetCurrentModel();
		Solid solid = (Solid)model;
		
		double diam = 1.00;
		
		if (solid == null)
		    throw new Exception("Msg: Solid model is null");
		
		ModelItems items = solid.ListItems (ModelItemType.ITEM_COORD_SYS);
		
		if (items == null || items.getarraysize() == 0)
		    throw new Exception("Msg: Items are null");
		
		CoordSystem csys = (CoordSystem)items.get(0);
		
		/* The instructions for the UDF creation */
		UDFCustomCreateInstructions instrs =
		    pfcUDFCreate.UDFCustomCreateInstructions_Create ("node");
		
		/* Make non-variant dimensions blank so they cannot be changed */
		instrs.SetDimDisplayType (UDFDimensionDisplayType.UDFDISPLAY_BLANK);
		
		/* Initialize the UDF reference and assign it to the instructions.
		   The string argument is the reference prompt for the particular
		   reference. */
		Selection csys_sel = 
		    pfcSelect.CreateModelItemSelection (csys, null);
		
		UDFReference csys_ref = pfcUDFCreate.UDFReference_Create ("REF_CSYS", csys_sel);
		
		UDFReferences refs = UDFReferences.create();
		
		refs.set (0, csys_ref);
		
		instrs.SetReferences (refs);
		
		/* Initialize the variant dimension and assign it to the instructions.
		   The string argument is the dimension symbol for the variant
		   dimension. */
		UDFVariantDimension var_diam = pfcUDFCreate.UDFVariantDimension_Create ("d11", diam);
		
		UDFVariantValues vals = UDFVariantValues.create();
		vals.set (0, (UDFVariantValue)var_diam);
		
		instrs.SetVariantValues (vals);
		
		/* 
		   We need the placement model for the UDF for the call to
		   CreateUDFGroup().
		   
		   If you were placing the UDF in a model other than the owner of the
		   coordinate system, the placement would need to be provided separately.  
		*/
		
		Solid placement_model = (Solid)csys.GetDBParent();
		
		FeatureGroup group = 
		    placement_model.CreateUDFGroup ((UDFCustomCreateInstructions)instrs);
		
	    }
	
	catch (Exception e)
	    {
		System.out.println("1: " + e);
	    }
	
    }
    
    
}
